﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Skynet.BusinessEntities;
using Microsoft.Practices.Unity;
using Infrastructure.Aspect.Services.XmlDataQueries;

namespace Skynet.BusinessLogic.BusinessComponents
{
    public class BCCustomerQueries : IBCCustomersQueries
    {
        [Dependency]
        public DataAccess.ICustomerRepository CustomerRepository { get; set; }

        [Dependency]
        public Skynet.DataAccess.IOrganizationRepository OrganizationRepository { get; set; }


        [Dependency]
        public IXmlDataQueriesService XmlDataQueriesService { get; set; }

        public IEnumerable<Customer> FindCustomers(CustomerFindOptions option = CustomerFindOptions.GetAll, Guid? id = null, string Name = null, string UserName = null)
        {
            IEnumerable<Customer> result = null;

            switch (option)
            {
                case CustomerFindOptions.GetOne:
                    var cus = this.CustomerRepository.GetOne(id);
                    if (cus != null)
                    {
                        result = new List<Customer>() { cus };
                    }
                    break;
                case CustomerFindOptions.GetAll:
                    result = this.CustomerRepository.GetAll();
                    break;
                case CustomerFindOptions.GetByUserName:
                    var cusUserName = this.CustomerRepository.GetOneByUserName(UserName);
                    result = new List<Customer>() { cusUserName };
                    break;
                default:
                    break;
            }
            return result;
        }

        public string SearchCustomers(CustomerSearchOption option = CustomerSearchOption.GetAll, Guid? id = null, string Name = null, string UserName = null)
        {
            return null;
        }

        
        public string QueryOrganizations(QueryOrganizationOptions option = QueryOrganizationOptions.GetAll,  Guid? CustomerId = null)
        {
            string Result = null; 
            switch (option)
            {
                case QueryOrganizationOptions.GetAll:
                    break;
                case QueryOrganizationOptions.GetAllByCustomerAdmin:
                    Result = this.XmlDataQueriesService.executeprocedure(Constants.OperationsNames.Organizations.GetAllbyCustomerAdmin, CustomerId);
                    break;
                default:

                    break;
            }
            return Result; 
        }
        public Organization FindOrganization(FindOrganizationOptions option = FindOrganizationOptions.FindById, Guid? OrganizationId = null)
        {
            Organization result = null;
            switch (option)
            {
                case FindOrganizationOptions.FindById:
                    result = this.OrganizationRepository.GetOne(OrganizationId);
                    break;
                default:
                    break;
            }
            return result; 
        }
    }
}
